package com.sauro.common.util;

import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 导出为ecxcel表格
 *
 * @author liuxk
 * @date 2022/11/30
 */
@Slf4j
public class EasyExcelHelper {

    /**
     * 输出Excel数据流
     * <p>
     * 设置请求头Content-Type = application/vnd.ms-excel，UTF8编码格式
     *
     * @param data     输出数据，List对象
     * @param head     数据表头，Class
     * @param fileName 输出文件名
     */
    public static void write(HttpServletResponse response, Class head, List data, String fileName) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            EasyExcel.write(response.getOutputStream(), head)
                    .sheet("sheet").doWrite(data);
        } catch (IOException e) {
            log.error("Excel导出数据异常", e);
        }
    }

    public static void main(String[] args) {
//        ArrayList
        String s = null;
        String a = "q" + "\n";
        String s1 = s + a;
        System.out.println(s1);
    }
}